###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# Index.pm.pl -> Index module                                                 #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Index;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Error.pm.pl";
require "Standard.pm.pl";

use strict;

require "Output/Index.pm.pl";

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
	my %in = ("DB" => undef, "STATIC" => 0, @_);

  #----------------------------------------------------------------------#
  # Getting data...                                                      #

  &Standard::ShowStatic(DB => $in{'DB'}, TYPE => "Index") unless ($in{'STATIC'});

  my ($Categories, $CategoriesIndex) = $in{'DB'}->Query(
    TABLE   => "Categories",
    SORT    => "FULL_TITLE",
    BY      => "A-Z"
  );

  my $Featured;
  if ($GENERAL->{'FEATURED'}) {
    my $resources;
    if (scalar(@{ $GENERAL->{'FEATURED_CATEGORIES'} }) >= 1) {
      ($resources, undef) = $in{'DB'}->Query(
        TABLE   => "Resources",
        WHERE   => {
          CATEGORY  => $GENERAL->{'FEATURED_CATEGORIES'}
        },
        MATCH   => "ANY",
        SORT    => "ID",
        BY      => "A-Z"
      );

      @{$resources} = grep { $_->{'STATUS'} && $_->{'HITS'} >= $GENERAL->{'FEATURED_MIN_HITS'} && $_->{'RATING'} >= $GENERAL->{'FEATURED_MIN_RATING'} } @{$resources};
    } else {
      ($resources, undef) = $in{'DB'}->Query(
        TABLE   => "Resources",
        WHERE   => {
          HITS    => [">".($GENERAL->{'FEATURED_MIN_HITS'} - 1)],
          RATING  => [">".($GENERAL->{'FEATURED_MIN_RATING'} - 1)],
          STATUS  => ["1"]
        },
        MATCH   => "ALL",
        SORT    => "ID",
        BY      => "A-Z"
      );
    }
  
    $Featured = (scalar(@{$resources}) >= 1 ? $resources->[int(rand(scalar(@{$resources})))] : "");
  }

  foreach my $category (@{ $Categories }) {
    next if ($category->{'ROOT'} eq "0");
    
    my @root = split(/,/, $category->{'ROOT'});
    my $root = $Categories->[$CategoriesIndex->{$root[1]}];

    $root->{'RESOURCES'} += $category->{'RESOURCES'};
    
    next if (scalar(@root) > 2);
    push(@{ $root->{'subcategories'} }, $category);
  }
  
  @{$Categories} = grep { $_->{'ROOT'} eq "0" } @{$Categories};

  my %INPUT;
  
  $INPUT{'FEATURED'}        = $Featured;
  $INPUT{'CATEGORIES'}      = $Categories;
  $INPUT{'TOTAL_RESOURCES'} = $in{'DB'}->Query(
    TABLE => "Resources",
    WHERE => {
      STATUS  => ["1"]
    },
    MATCH => "ALL",
    COUNT => 1
  );

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Skin = Skin::Index->new();

  return $Skin->show(input => \%INPUT, static => 1) if ($in{'STATIC'});

  &Standard::PrintHTMLHeader();
  print $Skin->show(input => \%INPUT);

  return 1;
}

1;